package myJs

import myJs.Utils._
import myJs.myPkg._
import myJs.myPkg.jquery._

import scala.scalajs.js
import scala.scalajs.js.annotation.{JSExport, JSExportTopLevel}
import myJs.Implicits._
import scala.scalajs.js.Dynamic.{global => g}
import scala.concurrent.ExecutionContext.Implicits.global

/**
 * Created by yz on 2019/4/15
 */
@JSExportTopLevel("Predict")
object Predict {

  @JSExport("init")
  def init = {
    bootStrapValidator
    //    showResult("322")

  }


  @JSExport("myRun")
  def myRun = {
    val bv = $("#form").bv
    bv.validate()
    val valid = bv.isValid().asInstanceOf[Boolean]
    if (valid) {
      val data = $("#form").serialize()
      val url = g.jsRoutes.controllers.PredictController.predict().url.toString
      val index = layer.alert(element, layerOptions)
      MyFetch.post(url = s"${url}", data = data, headers = Map("Content-Type" -> "application/x-www-form-urlencoded")).
        map { tmpRs =>
          layer.close(index)
          val rs = tmpRs.asInstanceOf[js.Dictionary[js.Any]]
          if (rs.myGet("valid") == "false") {
            Swal.swal(SwalOptions.title("错误").text(rs.myGet("message")).`type`("error"))
            $("#result").hide()
          } else {
            val missionId = rs.myGet("missionId")
            PredictResult.showResult(missionId)
          }
        }.timeoutDeal(index)
    }

  }


  def bootStrapValidator = {
    val dict = js.Dictionary(
      "feedbackIcons" -> js.Dictionary(
        "valid" -> "glyphicon glyphicon-ok",
        "invalid" -> "glyphicon glyphicon-remove",
        "validating" -> "glyphicon glyphicon-refresh",
      ),
      "fields" -> js.Dictionary(
        "sampleId" -> js.Dictionary(
          "validators" -> js.Dictionary(
            "notEmpty" -> js.Dictionary(
              "message" -> "样本号不能为空!"
            ),
          )
        ),
        "age" -> js.Dictionary(
          "validators" -> js.Dictionary(
            "notEmpty" -> js.Dictionary(
              "message" -> "年龄不能为空!"
            ),
            "between" -> js.Dictionary(
              "min" -> 18,
              "max" -> 100,
              "message" -> "年龄必须处于18到100之间！!"
            ),
          )
        ),
        "bmi" -> js.Dictionary(
          "validators" -> js.Dictionary(
            "notEmpty" -> js.Dictionary(
              "message" -> "体质指数不能为空!"
            ),
            "between" -> js.Dictionary(
              "min" -> 0,
              "max" -> 500,
              "message" -> "体质指数必须处于0到500之间！!"
            ),
          )
        ),
        "fbg" -> js.Dictionary(
          "validators" -> js.Dictionary(
            "notEmpty" -> js.Dictionary(
              "message" -> "空腹血糖不能为空!"
            ),
            "between" -> js.Dictionary(
              "min" -> 0,
              "max" -> 100,
              "message" -> "空腹血糖必须处于0到100之间！!"
            ),
          )
        ),
        "ggt" -> js.Dictionary(
          "validators" -> js.Dictionary(
            "notEmpty" -> js.Dictionary(
              "message" -> "γ-谷氨酰基转移酶不能为空!"
            ),
            "between" -> js.Dictionary(
              "min" -> 0,
              "max" -> 2000,
              "message" -> "γ-谷氨酰基转移酶必须处于0到2000之间！!"
            ),
          )
        ),
        "hdlch" -> js.Dictionary(
          "validators" -> js.Dictionary(
            "notEmpty" -> js.Dictionary(
              "message" -> "高密度脂蛋白胆固醇不能为空!"
            ),
            "between" -> js.Dictionary(
              "min" -> 0,
              "max" -> 100,
              "message" -> "高密度脂蛋白胆固醇必须处于0到100之间！!"
            ),
          )
        ),
        "tbil" -> js.Dictionary(
          "validators" -> js.Dictionary(
            "notEmpty" -> js.Dictionary(
              "message" -> "总胆红素不能为空!"
            ),
            "between" -> js.Dictionary(
              "min" -> 0,
              "max" -> 500,
              "message" -> "总胆红素必须处于0到500之间！!"
            ),
          )
        ),
        "albumin" -> js.Dictionary(
          "validators" -> js.Dictionary(
            "notEmpty" -> js.Dictionary(
              "message" -> "白蛋白不能为空!"
            ),
            "between" -> js.Dictionary(
              "min" -> 0,
              "max" -> 1000,
              "message" -> "白蛋白必须处于0到1000之间！!"
            ),
          )
        ),
        "ast" -> js.Dictionary(
          "validators" -> js.Dictionary(
            "notEmpty" -> js.Dictionary(
              "message" -> "天门冬氨酸氨基转移酶不能为空!"
            ),
            "between" -> js.Dictionary(
              "min" -> 0,
              "max" -> 1000,
              "message" -> "天门冬氨酸氨基转移酶必须处于0到1000之间！!"
            ),
          )
        ),
        "plt" -> js.Dictionary(
          "validators" -> js.Dictionary(
            "notEmpty" -> js.Dictionary(
              "message" -> "血小板计数不能为空!"
            ),
            "between" -> js.Dictionary(
              "min" -> 0,
              "max" -> 1000,
              "message" -> "血小板计数必须处于0到1000之间！!"
            ),
          )
        ),
      )
    )
    g.$("#form").bootstrapValidator(dict)

  }


}
